package com.sit.estatemanagementserver.controller;

import com.sit.estatemanagementserver.enity.Repair;
import com.sit.estatemanagementserver.enity.Response;
import com.sit.estatemanagementserver.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/repairs") // 重构: 路径统一
public class RepairController {
    @Autowired
    RepairService repairService;

    @PostMapping("/add")
    public Response<?> addRepair(@RequestBody Repair repair) {
        if (repairService.addRepair(repair)) {
            return Response.success("新建报修成功");
        } else {
            return Response.error("新建报修失败");
        }
    }

    @GetMapping("/room/{roomNumber}")
    public Response<List<Repair>> getRepairByRoomNumber(@PathVariable String roomNumber) {
        List<Repair> repairs = repairService.getRepairByRoomNumber(roomNumber);
        return Response.success(repairs);
    }

    // 重构: 使用PUT方法更新资源状态，更符合REST规范
    @PutMapping("/finish/{workOrderId}")
    public Response<?> finishRepair(@PathVariable String workOrderId) {
        if (repairService.finishRepair(workOrderId)) {
            return Response.success();
        } else {
            return Response.error("该报修不存在或已完成");
        }
    }
}